package com.jk.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.jk.dao.ClothDao;
import com.jk.entity.AreaEntity;
import com.jk.entity.UserEntity;
import com.jk.service.ClothService;
import com.jk.util.HttpClientUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.List;

import java.util.concurrent.TimeUnit;

/**
 * 类描述：
 *
 * @ClassName ClothServiceImpl
 * @Description TODO
 * @Author fandasen
 * @Date 2022/5/30 17:44
 * @Version 1.0
 */
@Service
@SuppressWarnings("all")
public class ClothServiceImpl implements ClothService {

    @Autowired
    private ClothDao clothDao;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 登录
     * @param user
     * @param sesssion
     * @return
     */
    @Override
    public Object login(UserEntity user, HttpSession sesssion) {
        String key = "login";
        //判断用户是否通过手机号登录
        if(user.getUserLoginNum().length()==11){
            //判断当前用户的账号与手机号是否对应
            UserEntity phone = clothDao.findPhoneToLogin(user.getUserLoginNum());
            if(phone==null){
                return "该手机号未注册";
            }
            if(!user.getUserLoginPwd().equals(phone.getUserLoginPwd())){
                return "密码错误，请重试";
            }
            redisTemplate.opsForValue().set(key, phone, 1, TimeUnit.DAYS);
            mongoTemplate.save(phone);
            /*sesssion.setAttribute("user2",phone);*/
            return "登陆成功";
        }else {
            UserEntity user2 = clothDao.login(user.getUserLoginNum());
            if (user2 == null) {
                return "账号不存在,请用手机号注册";
            }              //密码  password
            if (!user2.getUserLoginPwd().equals(user.getUserLoginPwd())) {
                return "密码错误,请重试";
            }
            redisTemplate.opsForValue().set(key, JSONObject.toJSONString(user2), 1, TimeUnit.DAYS);
            mongoTemplate.save(user2);
            /*sesssion.setAttribute("user2",user2);*/
            return "登陆成功";
        }
    }

    /**
     * 注册三级联动
     * @param pid
     * @return
     */
    @Override
    public List<AreaEntity> getprovince(Integer pid) {
        return clothDao.getprovince(pid);
    }




    /**
     * 发送验证码
     * @param phone
     * @return
     */
    @Override
    public String gainCode(String phone) {
        /*查找手机号*/
        UserEntity bean = clothDao.findPhone(phone);
        if(bean!=null){
            return "手机号已被注册";
        }
        String key = "code_"+phone;
        if(redisTemplate.hasKey(key)){
            return "验证码已发送过";
        }
        //发送key钥匙
        String appkey="LTAI5tPXb6T19QmZgcZrU6qu";
        //发送秘钥
        String appSecret="ndtzgszDM8eynOIzfKYHROtjYEXQJD";
        //短信模板id
        int templateid = 19504907;
        //调用短信接口
        JSONObject jsonObject = HttpClientUtil.sendCode(phone,appkey,appSecret,templateid);
        int code = jsonObject.getIntValue("code");
        if(code!=414){
            return "发送短信失败";
        }
        String nonce = jsonObject.getString("nonce");
        redisTemplate.opsForValue().set(key, nonce, 60, TimeUnit.MINUTES);
        return "短信发送成功";


    }

    /**
     * 手机号注册
     * @param user
     * @param session
     * @return
     */
    @Override
    public String phoneRegister(UserEntity user, HttpSession session) {
        UserEntity user2 = clothDao.findUserAccount(user.getUserLoginNum());
        String phone = user.getUserPhone();
        String key = "code_"+phone;
        if(!redisTemplate.hasKey(key)){
            return "请重新获取验证码";
        }
        String redisCode = (String) redisTemplate.opsForValue().get(key);
        if(!redisCode.equals(user.getCode())){
            return "验证码不一致，注册失败";
        }
        if(user.getUserLoginNum() == null || user.getUserLoginNum()==""){
            return "账号不能为空";
        }
        if(user.getUserLoginPwd() == null || user.getUserLoginPwd()==""){
            return "密码不能为空";
        }
        if(user2!=null){
            return "账号重复，请更换账号";
        }
        if(user.getUserLoginNum().length()>6){
            return "账号长度不能超过6位";
        }
        if(user.getUserLoginPwd().length()<6){
            return "密码不能小于6位";
        }
        if(!user.getPwdTwo().equals(user.getUserLoginPwd())){
            return "两次输入的密码不一致";
        }
        clothDao.phoneRegister(user);
        mongoTemplate.save(user);
        return "注册成功";
    }

    /**
     * 用户注册
     * @param user
     * @return
     */
    @Override
    public String register(UserEntity user) {
        UserEntity user2 = clothDao.findUserAccount(user.getUserLoginNum());
        if(user2!=null){
            return "账号重复，请更换账号";
        }
        if(user.getUserLoginNum()==null || user.getUserLoginNum()==""){
            return "账号不能为空";
        }
        if(user.getUserLoginPwd()==null || user.getUserLoginPwd()==""){
            return "密码不能为空";
        }
        if(user.getUserLoginNum().length()>6){
            return "账号长度不能超过6位";
        }
        if(user.getUserLoginPwd().length()<6){
            return "密码不能小于6位";
        }
        if(!user.getPwdTwo().equals(user.getUserLoginPwd())){
            return "两次输入的密码不一致";
        }
        clothDao.register(user);
        mongoTemplate.save(user);
        return "注册成功";
    }

}
